package yass.drawcard;

import android.Manifest;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.core.audioplayer.utils.LogUtil;

import java.io.File;
import java.lang.ref.SoftReference;

import cn.finalteam.galleryfinal.permission.EasyPermissions;
import yass.drawcard.base.BaseActivity;
import yass.drawcard.cameratakelib.CameraTakeManager;
import yass.drawcard.cameratakelib.listener.CameraTakeListener;

public class CameraActivity extends BaseActivity implements View.OnClickListener {

    private Context mContext;

    /** 权限相关*/
    private static final int GETPERMS = 100;
    private String[] perms;
    private Handler permissionsHandler = new Handler();

    private SurfaceView previewView;
    private ImageView imgPic;
    private TextView tvPicDir;
    private Button take_photo;

    CameraTakeManager manager;


    /**
     * 接收子线程消息的 Handler
     */
    public static class MyHandler extends Handler {

        // 软引用
        SoftReference<CameraActivity> fragmentRef;

        public MyHandler(CameraActivity fragment) {
            fragmentRef = new SoftReference<CameraActivity>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            CameraActivity fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }
            Bundle bundle = msg.getData();

            // 处理UI 变化
            switch (msg.what) {
                /*case ConstValues.WAIT1:// // 获取分类列表的回调
                    fragment.getJsonSuc(bundle);
                    break;*/
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        mContext = this;
        perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        checkPermission();

        initView();
        initData();
    }

    public void checkPermission() {
        //判断是否有相关权限，并申请权限
        if (EasyPermissions.hasPermissions(mContext, perms)) {
            permissionsHandler.post(new Runnable() {
                @Override
                public void run() {
                    init();
                }
            });
        } else {
            ActivityCompat.requestPermissions(CameraActivity.this, perms, GETPERMS);
        }
    }

    private void initView() {

        previewView = (SurfaceView) findViewById(R.id.surfaceview);
        imgPic = (ImageView) findViewById(R.id.img_pic);
        tvPicDir = (TextView) findViewById(R.id.tv_pic_dir);
        take_photo = (Button) findViewById(R.id.btn_take_photo);
        take_photo.setOnClickListener(this);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        init();
    }

    private void init(){
        manager = new CameraTakeManager(this, previewView, new CameraTakeListener() {
            @Override
            public void onSuccess(File bitmapFile, Bitmap mBitmap) {
                imgPic.setImageBitmap(mBitmap);
                tvPicDir.setText("图片路径：" + bitmapFile.getPath());
            }

            @Override
            public void onFail(String error) {
                LogUtil.e(error);
            }
        });
    }


    private void initData() {


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_take_photo:
                /** 点击拍照获取照片*/
                manager.takePhoto();
                break;
        }
    }
}
